package com.ljx.controller;


import com.ljx.R;
import com.ljx.pojo.EduChapter;
import com.ljx.pojo.chapter.ChapterVo;
import com.ljx.service.impl.EduChapterServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 李加喜
 * @since 2020-09-25
 */
@Api(description = "章节小节显示")
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {
    @Autowired
    private EduChapterServiceImpl eduChapterService;
    //要根据课程的Id进行查询  显示课程的章节信息
    @GetMapping("/getchapter/{courseId}")
    private R getChapter(@PathVariable String courseId){
        List<ChapterVo> list=eduChapterService.getChapterById(courseId);
        return R.ok().data("allChapterVideo",list);
    }
    //添加章节
    @ApiOperation("添加章节")
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        boolean b = eduChapterService.save(eduChapter);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }
    //修改章节
    //根据id查询章节
    @GetMapping("/getChapter/{chapterId}")
    public R getChapterById(@PathVariable String chapterId){
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);
    }
    //对章节进行修改
    @ApiOperation("章节修改")
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }
    //删除章节
    @DeleteMapping("/deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        // 注意删除章节的时候里面有小节
        // 两种情况
        // 第一种:删除章节以及章节下面的小节
        // 第二种:有小节就不允许删除
        // eduChapterService.removeById(chapterId);
        boolean flag=eduChapterService.deleteChapter(chapterId);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
}

